﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CommonClasses;
using System.Data.SqlClient;
using CardGame.Properties;
using System.IO;

namespace CardGame
{
    public partial class Form1 : Form
    {
        Deck myDeck = new Deck();
        Hand PlayerHand = new Hand();
        Hand DealerHand = new Hand();

        public Form1()
        {
            InitializeComponent();

            //initialize new deck.
            myDeck.Initialize();

            //Setup for a new game.
            NewGame();

        }


        private void btnHit_Click(object sender, EventArgs e)
        {
            	//Only allow 5 cards.
	            if(PlayerHand.Cards.Count == 5) 
	            {
		            MessageBox.Show("You already have five cards.");
		            return;
	            }

	            //Add another card to the hand.
                PlayerHand.Cards.Add(myDeck.GetCard());

	            //Update the hand's total.
                PlayerHand.CalculateTotal();

	            //Display the new hand total.
                tbPTotal.Text = PlayerHand.Total.ToString();
	
	            //Figure out which card was dealt and display that card.
	            if (PlayerHand.Cards.Count == 3) 
	            {
                    pbPCard3.ImageLocation = PlayerHand.Cards[2].Image;
                    pbPCard3.Visible = true;

	            }
	            else if(PlayerHand.Cards.Count == 4) 
	            {
                    pbPCard4.ImageLocation = PlayerHand.Cards[3].Image;
                    pbPCard4.Visible = true;
	            }
	            else if (PlayerHand.Cards.Count == 5) 
	            {
                    pbPCard5.ImageLocation = PlayerHand.Cards[4].Image;
                    pbPCard5.Visible = true;
	            }

                //See if you busted.
                if (PlayerHand.Total >= 21)
                {
                    MessageBox.Show("You busted.");
                    LogResult("Dealer");
                    LogDbResult("Dealer");
                    return;
                }
        }

        private void btnStand_Click(object sender, EventArgs e)
        {
            //Display the second dealer card when the player stands.
            pbDCard2.ImageLocation = DealerHand.Cards[1].Image;

            while ((DealerHand.Total <= 16) && (DealerHand.Cards.Count !=5))
            {
                //Deal the dealer a new card.
                DealDealerNextCard();
            }

            //See who wins.
            if (DealerHand.Total > 21)
            {
                MessageBox.Show("The dealer is over 21 and loses.");
                LogResult("Player");
                LogDbResult("Player");
            }
            else
            {
                if (PlayerHand.Total > DealerHand.Total)
                {
                    MessageBox.Show("The player wins.");



                    LogResult("Player");
                    LogDbResult("Player");
                }
                else
                {
                    MessageBox.Show("The dealer wins.");
                    LogResult("Dealer");
                    LogDbResult("Dealer");
                }
            }
        }

        private void btnDeal_Click(object sender, EventArgs e)
        {
            //Deal the player 2 cards.
            PlayerHand.Cards.Add(myDeck.GetCard());
            PlayerHand.Cards.Add(myDeck.GetCard());
            PlayerHand.CalculateTotal();

            //Display both player cards and total.
            pbPCard1.ImageLocation = PlayerHand.Cards[0].Image;
            pbPCard1.Visible = true;
            pbPCard2.ImageLocation = PlayerHand.Cards[1].Image;
            pbPCard2.Visible = true;
            tbPTotal.Text = PlayerHand.Total.ToString();

            //Deal the dealer 2 cards.
            DealerHand.Cards.Add(myDeck.GetCard());
            DealerHand.Cards.Add(myDeck.GetCard());
            DealerHand.CalculateTotal();

            //Display both dealer cards and total.
            pbDCard1.ImageLocation = DealerHand.Cards[0].Image;
            pbDCard1.Visible = true;
            pbDCard2.ImageLocation = @"CardImages\back.png";
            pbDCard2.Visible = true;
            tbDTotal.Text = DealerHand.Total.ToString();

            //Disable deal button until the next game.
            btnDeal.Enabled = false;
            btnHit.Enabled = true;
            btnStand.Enabled = true;

            //Check for blackjack on the deal.
            if (DealerHand.Total == 21)
            {
                pbDCard2.ImageLocation = DealerHand.Cards[1].Image;
                MessageBox.Show("Dealer has Blackjack. The dealer wins.");
                LogResult("Dealer");
                LogDbResult("Dealer");
            }
            else if (PlayerHand.Total == 21)
            {
                MessageBox.Show("Player has Blackjack. The player wins.");
                LogResult("Player");
                LogDbResult("Player");
            }
        }

        private void btnNewGame_Click(object sender, EventArgs e)
        {
            NewGame();
        }

        private void NewGame()
        {
            pbDCard1.Visible = false;
            pbDCard2.Visible = false;
            pbDCard3.Visible = false;
            pbDCard4.Visible = false;
            pbDCard5.Visible = false;
            pbPCard1.Visible = false;
            pbPCard2.Visible = false;
            pbPCard3.Visible = false;
            pbPCard4.Visible = false;
            pbPCard5.Visible = false;

            tbDTotal.Text = "";
            tbPTotal.Text = "";

            PlayerHand = new Hand();
            DealerHand = new Hand();

            if (myDeck.Cards.Count < 10)
            {
                MessageBox.Show("Less than 10 cards in the current deck. Creating a new deck.");

                //Initialize a new deck.
                myDeck.Initialize();
            }

            //re-enable the deal button and disable hit and stand.
            btnDeal.Enabled = true;
            btnHit.Enabled = false;
            btnStand.Enabled = false;
        }

        private void DealDealerNextCard()
        {
            //Only allow 5 cards.
            if (DealerHand.Cards.Count == 5)
            {
                MessageBox.Show("You already have five cards.");
                return;
            }

            //Add another card to the hand.
            DealerHand.Cards.Add(myDeck.GetCard());

            //Update the hand's total.
            DealerHand.CalculateTotal();

            //Display the new hand total.
            tbDTotal.Text = DealerHand.Total.ToString();

            //Figure out which card was dealt and display that card.
            if (DealerHand.Cards.Count == 3)
            {
                pbDCard3.ImageLocation = DealerHand.Cards[2].Image;
                pbDCard3.Visible = true;

            }
            else if (DealerHand.Cards.Count == 4)
            {
                pbDCard4.ImageLocation = DealerHand.Cards[3].Image;
                pbDCard4.Visible = true;
            }
            else if (PlayerHand.Cards.Count == 5)
            {
                pbDCard5.ImageLocation = DealerHand.Cards[4].Image;
                pbDCard5.Visible = true;
            }

        }

        private void LogResult(string winner)
        {
            //Disable hit and stand until the next game is dealt.
            btnHit.Enabled = false;
            btnStand.Enabled = false;

            StreamWriter sw = new StreamWriter("log.txt", true);
            sw.WriteLine("The winner is " + winner + " and the score is " + PlayerHand.Total.ToString() + " " + DealerHand.Total.ToString());
            sw.Close();
        }

        private void LogDbResult(string winner)
        {

            SqlConnection conn = new SqlConnection("Data Source=184.168.47.17;Initial Catalog=cpcc;User ID=cpcc;Password=cpcc1234");

            string sqltext = String.Format("insert into gameresults values ('{0}', {1}, {2}, '{3}', {4})",
                                            winner, DealerHand.Total.ToString(),
                                            PlayerHand.Total.ToString(), DateTime.Now, 0.00);

            SqlCommand command = new SqlCommand(sqltext, conn);

            conn.Open();
            int i = command.ExecuteNonQuery();
            conn.Close();

        }

    }
}